package com.supor.aiot.md_msg.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.lifecycle.Observer;

import com.android.baseconfig.base.RxBus3;
import com.supor.aiot.common.app.Constants;
import com.supor.aiot.common.base.BaseMVVMEnhanceFragment;
import com.supor.aiot.common.entity.Result;
import com.supor.aiot.common.helper.GlobalDataCache;
import com.supor.aiot.md_msg.databinding.FragmentInteractiveNotificBinding;
import com.supor.aiot.md_msg.entity.MsgCenterListResultBean;
import com.supor.aiot.md_msg.model.MsgCenterModel;

import io.reactivex.rxjava3.functions.Consumer;

/**
 * @author: zook
 * @date: 2021/10/15
 */
public class InteractiveNotificFragment extends BaseMVVMEnhanceFragment<MsgCenterModel, FragmentInteractiveNotificBinding> {

    public static InteractiveNotificFragment newInstance() {
        InteractiveNotificFragment fragment = new InteractiveNotificFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initData() {
        super.initData();
        bindingView.llMsgCommType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getContext(), ReplyMsgActivity.class));
            }
        });
        bindingView.llMsgCollectionType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getContext(), LikeMsgActivity.class));
            }
        });
        RxBus3.getInstance().register(Constants.Tag.MSG_COMM_READ, (Consumer<String>) s -> {
            isLoaded = false;
        }, this.getClass());

        viewModel.getReplyMsgListUnreadNumResult().observe(this, new Observer<Result<MsgCenterListResultBean>>() {
            @Override
            public void onChanged(Result<MsgCenterListResultBean> msgCenterListResultBeanResult) {
                if (null != msgCenterListResultBeanResult.getData() &&
                        msgCenterListResultBeanResult.getData().getTotal() > 0) {
                    bindingView.viewCommUnread.setVisibility(View.VISIBLE);
                } else {
                    bindingView.viewCollectionUnread.setVisibility(View.GONE);
                }
            }
        });
        viewModel.getLikeMsgListUnreadNumResult().observe(this, new Observer<Result<MsgCenterListResultBean>>() {
            @Override
            public void onChanged(Result<MsgCenterListResultBean> msgCenterListResultBeanResult) {
                if (null != msgCenterListResultBeanResult.getData() &&
                        msgCenterListResultBeanResult.getData().getTotal() > 0) {
                    bindingView.viewCollectionUnread.setVisibility(View.VISIBLE);
                } else {
                    bindingView.viewCollectionUnread.setVisibility(View.GONE);
                }
            }
        });
//        viewModel.getReplyMsgListUnreadNum(GlobalDataCache.userInfo.getBossUserId(),
//                bindDestoryAutoDispose()
//        );
        viewModel.getLikeMsgListUnreadNum(GlobalDataCache.userInfo.getBossUserId(),
                bindDestoryAutoDispose()
        );
        RxBus3.getInstance().register(Constants.Tag.MSG_COMM_READ, (Consumer<String>) s -> {
            viewModel.getLikeMsgListUnreadNumResult().observe(this, new Observer<Result<MsgCenterListResultBean>>() {
                @Override
                public void onChanged(Result<MsgCenterListResultBean> msgCenterListResultBeanResult) {
                    if (null != msgCenterListResultBeanResult.getData() &&
                            msgCenterListResultBeanResult.getData().getTotal() > 0) {
                        bindingView.viewCollectionUnread.setVisibility(View.VISIBLE);
                    } else {
                        bindingView.viewCollectionUnread.setVisibility(View.GONE);
                    }
                }
            });
        }, this.getClass());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        RxBus3.getInstance().unregister(this.getClass());
    }
}